<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .wrap {
            width: 400px;
            height: 400px;
            margin: 50px auto;
            border: 1px solid #ccc;
        }

        .wrap .tab {
            height: 50px;
            border-bottom: 1px solid #ccc;
        }

        .wrap .tab .tab-item {
            float: left;
            width: 25%;
            height: 100%;
            line-height: 50px;
            text-align: center;
        }

        .wrap .tab .tab-item.active {
            background-color: pink;
            color: #fff;
        }

        .wrap .page {
            height: 350px;
        }

        .wrap .page .page-list {
            position: relative;
            height: 100%;
        }

        .wrap .page .page-item {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            text-align: center;
            line-height: 350px;
            font-size: 24px;
        }

        .wrap .page .page-item.active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="tab">
            <ul class="tab-list">
                <li class="tab-item active">选项1</li>
                <li class="tab-item">选项2</li>
                <li class="tab-item">选项3</li>
                <li class="tab-item">选项4</li>
            </ul>
        </div>
        <div class="page">
            <ul class="page-list">
                <li class="page-item active">页面1</li>
                <li class="page-item">页面2</li>
                <li class="page-item">页面3</li>
                <li class="page-item">页面4</li>
            </ul>
        </div>
    </div>


    <script type="text/javascript" src="utils.js"></script>
    <script type="text/javascript">
        ; (function () {
            var curIdx = 0;

            var Tab = function () {
                this.tabList = document.getElementsByClassName('tab-list')[0];
                this.pageList = document.getElementsByClassName('page-list')[0];
                this.tabItems = this.tabList.getElementsByClassName('tab-item');
                this.pageItems = this.pageList.getElementsByClassName('page-item');
            };

            Tab.prototype = {
                init: function () {
                    this.bindEvent();
                },
                bindEvent: function () {
                    self = this;
                    this.tabList.addEventListener('click', this.addActive.bind(this), false);
                },
                addActive: function (e) {
                    var e = e || window.event,
                        tar = e.target || e.srcEvent,
                        thisIdx = null,
                        otabItems = this.tabItems,
                        opageItems = this.pageItems,
                        len = otabItems.length;

                    otabItems[curIdx].className = 'tab-item';
                    opageItems[curIdx].className = 'page-item';
                    thisIdx = Array.prototype.indexOf.call(otabItems, tar);
                    curIdx = thisIdx;
                    otabItems[curIdx].className += ' active';
                    opageItems[curIdx].className += ' active';
                }
            }

            return new Tab().init();
        })();
    </script>









</body>

</html>